package cc.vv.btpartyjob.jnimodule.activity

import android.content.Intent
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.http.BaseHttpRequest
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.R
import cc.vv.btpartyjob.jnimodule.adapter.JLSimpleTreeAdapter
import cc.vv.btpartyjob.jnimodule.api.JniModuleAPI
import cc.vv.btpartyjob.jnimodule.binder.BaseBinder
import cc.vv.btpartyjob.jnimodule.delegate.OrganizationActivityDelegate
import cc.vv.btpartyjob.jnimodule.globle.IntentJniKey
import cc.vv.btpartyjob.jnimodule.moudel.bean.org.*
import cc.vv.btpartyjob.jnimodule.moudel.bean.tree.Node
import cc.vv.btpartyjob.jnimodule.moudel.bean.tree.TreeListViewAdapter
import cc.vv.btpartyjob.jnimodule.moudel.bean.tree.ZtreeNodesBean
import cc.vv.btpartyjob.jnimodule.moudel.req.JLOrgRequestObj
import cc.vv.btpartyjob.jnimodule.moudel.resp.JLOrgBtQuResponseObj
import cc.vv.btpartyjob.jnimodule.moudel.resp.OrgBtQuResponseObj
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 级联组织查询
 * Created by Chen.qi on 2018/10/30.
 */
class JLOrganizationActivity : BaseActivityMVP<OrganizationActivityDelegate>() {
    private var mAdapter: JLSimpleTreeAdapter? = null
    private var list = ArrayList<Node>()
    private var defaultLevel: Int = 2
    override fun initData() {
        viewDelegate?.setTopBarTitle("组织架构")
        list = ArrayList()
        mAdapter = JLSimpleTreeAdapter(viewDelegate?.getListView(), this, list, defaultLevel)
        viewDelegate?.getListView()?.adapter = mAdapter
        try {
            val id = intent.getStringExtra(IntentJniKey.INTENT_KEY_SEARCH_CHILD_LIST_ID)
            val type = intent.getStringExtra(IntentJniKey.INTENT_KEY_SEARCH_CHILD_LIST_TYPE)
            getAllOrgData(id, type)
        } catch (e: Exception) {
        }

    }


    /**
     * 获取所有组织结构数据
     */
    private fun getAllOrgData(id: String?, type: String?) {
        val path = JniModuleAPI.jiLianTree
        val params = JLOrgRequestObj()
        params.parentId = id
        params.types = type
        BaseHttpRequest.getRequest(path, params, JLOrgBtQuResponseObj::class.java, true)
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
            }
        })


        mAdapter?.setOnTreeNodeClickListener(object : TreeListViewAdapter.OnTreeNodeClickListener {
            override fun onClick(node: Node?, position: Int) {
                if (node?.isLeaf!!) {
                    for (i in list.indices) {
                        list[i].ztreeNodesBean.isCheck = node.id == list[i].id
                    }
                    mAdapter?.notifyDataSetChanged()
                }
            }
        })


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //确定
            var isCheck = false
            var orgId: String? = ""
            var orgName: String? = ""
            var orgType: String? = ""
            for (i in list.indices) {
                val ztreeNodesBean = list[i].ztreeNodesBean
                if (ztreeNodesBean?.isCheck!!) {
                    orgId = ztreeNodesBean.id
                    orgName = ztreeNodesBean.name
                    orgType = ztreeNodesBean.type
                    isCheck = true
                    break
                }
            }

            if (!isCheck) {
                LKToastUtil.showToastShort("请选择组织")
                return@OnClickListener
            }
            val intent = Intent()
            intent.putExtra(IntentJniKey.INTENT_KEY_ORGAN_ID, orgId)
            intent.putExtra(IntentJniKey.INTENT_KEY_ORGAN_NAME, orgName)
            intent.putExtra(IntentJniKey.INTENT_KEY_ORGAN_TYPE, orgType)
            setResult(101, intent)
            finish()
        }, R.id.rl_orGan_submit)
    }


    override fun getDelegateClass(): Class<OrganizationActivityDelegate> {
        return OrganizationActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()


    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is JLOrgBtQuResponseObj -> {
                //所有宝塔区数据
                val data = obj.data

                if (data != null && data.size > 0) {
                    for (i in 0 until data.size) {
                        val jiLianOneBean = data[i]
                        val id = jiLianOneBean.id
                        val name = jiLianOneBean.name
                        val type = jiLianOneBean.type
                        val pId = jiLianOneBean.parentId
                        val path = jiLianOneBean.path
                        val node = Node(id, pId, ZtreeNodesBean(id, pId, name, "$type", path))
                        list.add(node)
                    }
                }
//                val listFilter = ArrayList<Node>()
//                listFilter.addAll(list)
                mAdapter?.setNodeData(list, 2)
                mAdapter?.notifyDataSetChanged()
            }
        }
    }


    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
    }
}